package com.syezon.wifikey.news.adapter;

import android.app.Activity;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.blankj.utilcode.util.AppUtils;
import com.blankj.utilcode.util.LogUtils;
import com.bytedance.sdk.openadsdk.TTAdDislike;
import com.bytedance.sdk.openadsdk.TTAppDownloadListener;
import com.bytedance.sdk.openadsdk.TTNativeExpressAd;
import com.hodanet.appadvplatclient.ReportAgent;
import com.hodanet.appadvplatclient.ad.AdInfo;

import com.qq.e.ads.nativ.NativeExpressADView;
import com.syezon.wifikey.R;
import com.syezon.wifikey.ad.AdUtils;
import com.syezon.wifikey.channel.ChannelConfig;
import com.syezon.wifikey.imageloader.ImageLoader;
import com.syezon.wifikey.news.info.YiYouLiaoNewsInfoBean;
import com.syezon.wifikey.news.info.YiYouLiaoNewsListBean;
import com.syezon.wifikey.utils.ScreenUtil;
import com.syezon.wifikey.web.WebHelper;

import java.util.ArrayList;
import java.util.List;


/**
 *
 */
public class YiYouLiaoNewsAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context mContext;
    private List<YiYouLiaoNewsInfoBean> mData = new ArrayList<>();
    private TopAdListAdapter mTopAdListAdapter;
    private int screenWidth;
//    private List<AdInfo> mCurrentTopAdList;

    public YiYouLiaoNewsAdapter(Context context) {
        this.mContext = context;
        mTopAdListAdapter = new TopAdListAdapter(context);
        screenWidth = ScreenUtil.getScreenWidth(mContext);
    }

    public void setData(List<YiYouLiaoNewsInfoBean> data) {
        if (data != null && data.size() > 0) {
            mData.clear();
            mData.addAll(data);
//            if (mCurrentTopAdList != null) {
//                setTopAdInfo(mCurrentTopAdList);
//            }
            notifyDataSetChanged();
        }
    }

    public void addData(List<YiYouLiaoNewsInfoBean> data) {
        if (data != null && data.size() > 0) {
            mData.addAll(data);
            notifyDataSetChanged();
        }
    }

//    public void setTopAdInfo(List<AdInfo> adInfo) {
//        if (adInfo != null && adInfo.size() > 0) {
//            Iterator<YiYouLiaoNewsListBean.DataBean.InfosBean> iterator = mData.iterator();
//            while (iterator.hasNext()) {
//                YiYouLiaoNewsListBean.DataBean.InfosBean next = iterator.next();
//                if (next.getType() == NewsItemInfo.TYPE_TOP_AD_LIST) {
//                    iterator.remove();
//                }
//            }
//            NewsItemInfo newsItemInfo = new NewsItemInfo();
//            newsItemInfo.setType(NewsItemInfo.TYPE_TOP_AD_LIST);
//            newsItemInfo.setTopAdList(adInfo);
//            mData.add(0, newsItemInfo);
//            mCurrentTopAdList = adInfo;
//            notifyDataSetChanged();
//        }
//    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        switch (viewType) {
            case YiYouLiaoNewsInfoBean.TYPE_NEWS_BIG_IMG:
                return new BigPicItemViewHolder(LayoutInflater.from(mContext).inflate(R.layout.app_news_item_bigpic, parent, false));
            case YiYouLiaoNewsInfoBean.TYPE_NEWS_SINGLE_IMG:
                return new PicTextItemViewHolder(LayoutInflater.from(mContext).inflate(R.layout.app_news_item_text_pic, parent, false));
            case YiYouLiaoNewsInfoBean.TYPE_NEWS_NO_IMG:
                return new SimpleTextItemViewHolder(LayoutInflater.from(mContext).inflate(R.layout.app_news_item_simple_text, parent, false));
            case YiYouLiaoNewsInfoBean.TYPE_NEWS_THREDD_IMG:
                return new SmallPicItemViewHolder(LayoutInflater.from(mContext).inflate(R.layout.app_news_item_hot, parent, false));
            case YiYouLiaoNewsInfoBean.TYPE_TOP_AD_LIST:
                return new TopAdItemViewHolder(LayoutInflater.from(mContext).inflate(R.layout.app_news_item_top_ad, parent, false));
            case YiYouLiaoNewsInfoBean.TYPE_AD_CSJ:
                return new AdCSJItemViewHolder(LayoutInflater.from(mContext).inflate(R.layout.app_news_item_empty, parent, false));
            case YiYouLiaoNewsInfoBean.TYPE_AD_SELF:
                return new AdSelfItemViewHolder(LayoutInflater.from(mContext).inflate(R.layout.app_news_item_self, parent, false));
            case YiYouLiaoNewsInfoBean.TYPE_AD_GDT:
                return new GDTItemViewHolder(LayoutInflater.from(mContext).inflate(R.layout.app_news_item_gdt, parent, false));
            default:
                break;
        }
        return new DefaultItemViewHolder(new View(mContext));

    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        YiYouLiaoNewsInfoBean yiYouLiaoNewsInfoBean = mData.get(position);
        YiYouLiaoNewsListBean.DataBean.InfosBean newsInfo = yiYouLiaoNewsInfoBean.getInfosBean();
//            List<AdInfo> topAdList = yiYouLiaoNewsInfoBean.getTopAdList();
        if (holder instanceof BigPicItemViewHolder && newsInfo != null) {
            BigPicItemViewHolder viewHolder = (BigPicItemViewHolder) holder;
            onBindBigViewHolder(viewHolder, newsInfo);
        }
        if (holder instanceof SmallPicItemViewHolder && newsInfo != null) {
            SmallPicItemViewHolder viewHolder = (SmallPicItemViewHolder) holder;
            onBindSmallPicItemViewHolder(viewHolder, newsInfo);
        }
        if (holder instanceof SimpleTextItemViewHolder && newsInfo != null) {
            SimpleTextItemViewHolder viewHolder = (SimpleTextItemViewHolder) holder;
            onBindVSimpleTextItemViewHolder(viewHolder, newsInfo);
        }
        if (holder instanceof PicTextItemViewHolder && newsInfo != null) {
            PicTextItemViewHolder viewHolder = (PicTextItemViewHolder) holder;
            onBindPicTextViewHolder(viewHolder, newsInfo);
        }

        if (holder instanceof AdCSJItemViewHolder) {
            AdCSJItemViewHolder viewHolder = (AdCSJItemViewHolder) holder;
            onBindCSJAdViewHolder(viewHolder, yiYouLiaoNewsInfoBean);
        }

        if (holder instanceof AdSelfItemViewHolder) {
            AdSelfItemViewHolder viewHolder = (AdSelfItemViewHolder) holder;
            onBindSelfAdViewHolder(viewHolder, yiYouLiaoNewsInfoBean.getAdInfo());
        }

        if (holder instanceof GDTItemViewHolder) {
            GDTItemViewHolder viewHolder = (GDTItemViewHolder) holder;
            onBindGDTAdViewHolder(viewHolder, yiYouLiaoNewsInfoBean.getNativeExpressADView());
        }
    }

    private void onBindGDTAdViewHolder(GDTItemViewHolder viewHolder, NativeExpressADView contentAdData) {
        contentAdData.render();
        viewHolder.flContainer.removeAllViews();
        viewHolder.flContainer.addView(contentAdData);
    }

    private void onBindSelfAdViewHolder(AdSelfItemViewHolder viewHolder, final AdInfo adInfo) {
        viewHolder.tvNewsContent.setText(adInfo.getName());
        ImageLoader.getInstance().displayImage(mContext, adInfo.getPic(), viewHolder.imgPic, R.drawable.app_news_ic_news_default);
        viewHolder.tvDate.setVisibility(View.VISIBLE);
        viewHolder.tvDate.setText("广告");
        viewHolder.contentView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AdUtils.adClicked(mContext,adInfo);
            }
        });
    }

    private void onBindCSJAdViewHolder(final AdCSJItemViewHolder viewHolder, final YiYouLiaoNewsInfoBean newsInfo) {
        TTNativeExpressAd ttNativeExpressAd = newsInfo.getTtNativeExpressAd();
        bindDislike(viewHolder, ttNativeExpressAd);
        ttNativeExpressAd.setExpressInteractionListener(new TTNativeExpressAd.ExpressAdInteractionListener() {
            boolean reportShow;

            @Override
            public void onAdClicked(View view, int i) {
                if (!reportShow) {
                    ReportAgent.reportAdvClick(newsInfo.getAdInfo(), ChannelConfig.getChannel(), AppUtils.getAppVersionCode());
                    reportShow = true;
                }
            }

            @Override
            public void onAdShow(View view, int i) {
                if (!reportShow) {
                    reportShow = true;
                    ReportAgent.reportAdvExtShow(newsInfo.getAdInfo(), ChannelConfig.getChannel(), AppUtils.getAppVersionCode());
                }
            }

            @Override
            public void onRenderFail(View view, String s, int i) {
            }

            @Override
            public void onRenderSuccess(View view, float width, float height) {
                LogUtils.e("onRenderSuccess:    width:  " + width + "  height: " + height);
                viewHolder.flContainer.removeAllViews();
                viewHolder.flContainer.addView(view);
            }
        });

        ttNativeExpressAd.setDownloadListener(new TTAppDownloadListener() {
            boolean reportDownload;
            boolean reportInstall;

            @Override
            public void onIdle() {

            }

            @Override
            public void onDownloadActive(long l, long l1, String s, String s1) {

            }

            @Override
            public void onDownloadPaused(long l, long l1, String s, String s1) {

            }

            @Override
            public void onDownloadFailed(long l, long l1, String s, String s1) {

            }

            @Override
            public void onDownloadFinished(long l, String s, String s1) {
                if (!reportDownload) {
                    reportDownload = true;
                    ReportAgent.reportAdvDownload(newsInfo.getAdInfo(), ChannelConfig.getChannel(), AppUtils.getAppVersionCode());
                }
            }

            @Override
            public void onInstalled(String s, String s1) {
                if (!reportInstall) {
                    reportInstall = true;
//                    SDKReportHelper.reportSDKAdv(SDKReportHelper.SOURCE_CSJ, TTAdManagerHolder.YI_YOU_LIAO_NEWS, SDKReportHelper.OP_TYPE_INSTALL);
                    ReportAgent.reportAdvInstall(newsInfo.getAdInfo(),ChannelConfig.getChannel(),AppUtils.getAppVersionCode());
                }
            }
        });

        ttNativeExpressAd.render();
    }

    private void onBindTopAdViewHolder(TopAdItemViewHolder viewHolder, List<AdInfo> topAdList) {
        mTopAdListAdapter.setData(topAdList);
    }

    private void onBindPicTextViewHolder(PicTextItemViewHolder viewHolder, final YiYouLiaoNewsListBean.DataBean.InfosBean newsInfo) {
        viewHolder.tvNewsContent.setText(newsInfo.getTitle());
        ImageLoader.getInstance().displayImage(mContext, newsInfo.getImgs().get(0).getUrl(), viewHolder.imgPic, R.drawable.app_news_ic_news_default);
//        if (newsInfo.type == 0) {
//            viewHolder.tvTuiGuang.setVisibility(View.VISIBLE);
//        } else {
//            viewHolder.tvTuiGuang.setVisibility(View.GONE);
//        }
//        if (newsInfo.author.contains("众联广告")) {
//            viewHolder.tvTuiGuang.setVisibility(View.VISIBLE);
//            viewHolder.tvAuthor.setVisibility(View.GONE);
//        } else {
//            viewHolder.tvTuiGuang.setVisibility(View.GONE);
//            viewHolder.tvAuthor.setText(newsInfo.author);
//            viewHolder.tvAuthor.setVisibility(View.VISIBLE);
//        }
        viewHolder.tvDate.setText(newsInfo.getPublishTime());
        viewHolder.tvDate.setVisibility(View.VISIBLE);
        viewHolder.contentView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                goToWeb(mContext, newsInfo);
            }
        });
    }

    private void onBindVSimpleTextItemViewHolder(SimpleTextItemViewHolder viewHolder, final YiYouLiaoNewsListBean.DataBean.InfosBean newsInfo) {
        viewHolder.tvNewsContent.setText(newsInfo.getTitle());
        viewHolder.tvNewsDate.setText(newsInfo.getPublishTime());
        viewHolder.tvNewsSource.setText(newsInfo.getSource());
        viewHolder.contentView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                goToWeb(mContext, newsInfo);
            }
        });
    }

    private void onBindSmallPicItemViewHolder(SmallPicItemViewHolder viewHolder, final YiYouLiaoNewsListBean.DataBean.InfosBean newsInfo) {
        viewHolder.tvNewsContent.setText(newsInfo.getTitle());
        if (newsInfo.getImgs() != null && !newsInfo.getImgs().isEmpty()) {
            ImageLoader.getInstance().displayImage(mContext, newsInfo.getImgs().get(0).getUrl(), viewHolder.imgLeft, R.drawable.app_news_ic_news_default);
            ImageLoader.getInstance().displayImage(mContext, newsInfo.getImgs().get(1).getUrl(), viewHolder.imgCenter, R.drawable.app_news_ic_news_default);
            ImageLoader.getInstance().displayImage(mContext, newsInfo.getImgs().get(2).getUrl(), viewHolder.imgRight, R.drawable.app_news_ic_news_default);
        }
        viewHolder.tvAuthor.setText(newsInfo.getSource());
        viewHolder.tvDate.setVisibility(View.VISIBLE);
        viewHolder.tvDate.setText(newsInfo.getPublishTime());
        viewHolder.contentView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                goToWeb(mContext, newsInfo);
            }
        });
    }

    private void onBindBigViewHolder(BigPicItemViewHolder viewHolder, final YiYouLiaoNewsListBean.DataBean.InfosBean newsInfo) {
        viewHolder.tvNewsContent.setText(newsInfo.getTitle());
        if (newsInfo.getImgs() != null && !newsInfo.getImgs().isEmpty()) {
            ImageLoader.getInstance().displayImage(mContext, newsInfo.getImgs().get(0).getUrl(), viewHolder.imgBigPic, R.drawable.app_news_ic_news_default);
        }
        if (newsInfo.getImgType() == 0) {
            viewHolder.tvNewsTuiGuang.setVisibility(View.VISIBLE);
            viewHolder.tvAuthor.setVisibility(View.GONE);
        } else {
            viewHolder.tvNewsTuiGuang.setVisibility(View.GONE);
            viewHolder.tvAuthor.setVisibility(View.VISIBLE);
            viewHolder.tvAuthor.setText(newsInfo.getSource());
        }
        viewHolder.contentView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                goToWeb(mContext, newsInfo);
            }
        });
    }

    private void bindDislike(final AdCSJItemViewHolder viewHolder, final TTNativeExpressAd ad) {
        //使用默认模板中默认dislike弹出样式
        ad.setDislikeCallback((Activity) mContext, new TTAdDislike.DislikeInteractionCallback() {
            @Override
            public void onSelected(int position, String value) {
                mData.remove(viewHolder.getLayoutPosition());
                notifyDataSetChanged();
            }

            @Override
            public void onCancel() {
            }
        });
    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    @Override
    public int getItemViewType(int position) {
        return mData.get(position).getType();
    }

    private void goToWeb(Context context, final YiYouLiaoNewsListBean.DataBean.InfosBean newsInfo) {
        if (newsInfo != null) {
            WebHelper.showAd(context, newsInfo.getH5DetailLink(), newsInfo.getTitle());
        }
    }

    class BigPicItemViewHolder extends RecyclerView.ViewHolder {
        View contentView;
        TextView tvNewsContent;
        ImageView imgBigPic;
        TextView tvNewsTuiGuang;
        TextView tvAuthor;

        BigPicItemViewHolder(View itemView) {
            super(itemView);
            contentView = itemView;
            tvNewsContent = (TextView) itemView.findViewById(R.id.item_news_content);
            imgBigPic = (ImageView) itemView.findViewById(R.id.item_news_img);
            ScreenUtil.adapterScreen(imgBigPic, screenWidth, (float) 0.42);
            tvNewsTuiGuang = (TextView) itemView.findViewById(R.id.item_news_tuiguang);
            tvAuthor = (TextView) itemView.findViewById(R.id.item_news_source);
        }
    }

    class SmallPicItemViewHolder extends RecyclerView.ViewHolder {
        View contentView;
        TextView tvNewsContent;
        ImageView imgLeft, imgCenter, imgRight;
        TextView tvNewsTuiGuang;
        TextView tvAuthor;
        TextView tvDate;

        SmallPicItemViewHolder(View itemView) {
            super(itemView);
            contentView = itemView;
            int width = (screenWidth - ScreenUtil.dipTopx(mContext, 40)) / 3;
            tvNewsContent = (TextView) itemView.findViewById(R.id.item_news_content);
            imgLeft = (ImageView) itemView.findViewById(R.id.news_image_left);
            imgCenter = (ImageView) itemView.findViewById(R.id.news_image_center);
            imgRight = (ImageView) itemView.findViewById(R.id.news_image_right);
            tvNewsTuiGuang = (TextView) itemView.findViewById(R.id.item_news_tuiguang);
            tvAuthor = (TextView) itemView.findViewById(R.id.item_news_source);
            tvDate = (TextView) itemView.findViewById(R.id.item_news_date);
            ScreenUtil.adapterScreen(imgLeft, width, (float) 0.75);
            ScreenUtil.adapterScreen(imgCenter, width, (float) 0.75);
            ScreenUtil.adapterScreen(imgRight, width, (float) 0.75);
        }
    }

    class SimpleTextItemViewHolder extends RecyclerView.ViewHolder {
        View contentView;
        TextView tvNewsContent;
        TextView tvNewsDate;
        TextView tvNewsSource;

        SimpleTextItemViewHolder(View itemView) {
            super(itemView);
            contentView = itemView;
            tvNewsContent = (TextView) itemView.findViewById(R.id.item_news_content);
            tvNewsDate = (TextView) itemView.findViewById(R.id.item_news_date);
            tvNewsSource = (TextView) itemView.findViewById(R.id.item_news_source);
        }
    }

    class PicTextItemViewHolder extends RecyclerView.ViewHolder {
        View contentView;
        TextView tvNewsContent;
        ImageView imgPic;
        TextView tvTuiGuang;
        TextView tvAuthor;
        TextView tvDate;

        PicTextItemViewHolder(View itemView) {
            super(itemView);
            contentView = itemView;
            tvNewsContent = (TextView) itemView.findViewById(R.id.item_news_content);
            imgPic = (ImageView) itemView.findViewById(R.id.item_news_img);
            tvTuiGuang = (TextView) itemView.findViewById(R.id.item_news_tuiguang);
            tvAuthor = (TextView) itemView.findViewById(R.id.item_news_source);
            tvDate = (TextView) itemView.findViewById(R.id.item_news_date);
            int width = (screenWidth - ScreenUtil.dipTopx(mContext, 40)) / 3;
            ScreenUtil.adapterScreen(imgPic, width, (float) 0.75);
        }
    }

    class TopAdItemViewHolder extends RecyclerView.ViewHolder {

        RecyclerView rvAdList;

        TopAdItemViewHolder(View itemView) {
            super(itemView);
            rvAdList = (RecyclerView) itemView.findViewById(R.id.rv_ad_list);
            rvAdList.setLayoutManager(new GridLayoutManager(mContext, 4));
            rvAdList.setAdapter(mTopAdListAdapter);
        }
    }

    class AdCSJItemViewHolder extends RecyclerView.ViewHolder {

        private final FrameLayout flContainer;

        AdCSJItemViewHolder(View itemView) {
            super(itemView);
            flContainer = (FrameLayout) itemView.findViewById(R.id.fl_container);
        }
    }

    class AdSelfItemViewHolder extends RecyclerView.ViewHolder {
        View contentView;
        TextView tvNewsContent;
        ImageView imgPic;
        TextView tvTuiGuang;
        TextView tvAuthor;
        TextView tvDate;

        AdSelfItemViewHolder(View itemView) {
            super(itemView);
            contentView = itemView;
            tvNewsContent = (TextView) itemView.findViewById(R.id.item_news_content);
            imgPic = (ImageView) itemView.findViewById(R.id.item_news_img);
            tvTuiGuang = (TextView) itemView.findViewById(R.id.item_news_tuiguang);
            tvAuthor = (TextView) itemView.findViewById(R.id.item_news_source);
            tvDate = (TextView) itemView.findViewById(R.id.item_news_date);
            int width = (screenWidth - ScreenUtil.dipTopx(mContext, 40)) / 3;
            ScreenUtil.adapterScreen(imgPic, width, (float) 0.75);
        }
    }

    class DefaultItemViewHolder extends RecyclerView.ViewHolder {
        DefaultItemViewHolder(View itemView) {
            super(itemView);
        }
    }

    class GDTItemViewHolder extends RecyclerView.ViewHolder {
        private final FrameLayout flContainer;

        GDTItemViewHolder(View itemView) {
            super(itemView);
            flContainer = (FrameLayout) itemView.findViewById(R.id.fl_container);
        }
    }

}
